//
// Created by Administrator on 2024/4/9.
//
#include<bits/stdc++.h>

using namespace std;
using ll = long long;
#define endl '\n'
const int maxn = 20 + 5;
int n = 5, m = 5;
int G[maxn][maxn];
int book[maxn][maxn];
int dir[][2] = {0, 1, 0, -1, 1, 0, -1, 0};

int Q[maxn * maxn + 500][2], head = 0, tail = -1;

bool bfs(int sx, int sy) {
    head = 0;
    tail = -1;
    ++tail;
    Q[tail][0] = sx;
    Q[tail][1] = sy;
    book[sx][sy] = 1;

    while (head <= tail) {
        int hx = Q[head][0];
        int hy = Q[head][1];
        ++head;
        cout << G[hx][hy] << " ";
        for (int i = 0; i < 4; i++) {
            int nx = hx + dir[i][0];
            int ny = hy + dir[i][1];
            if (nx < 0 || nx >= n || ny < 0 || ny >= m || book[nx][ny] == 1 || G[nx][ny] == 1)continue;
            ++tail;
            Q[tail][0] = nx;
            Q[tail][1] = ny;
            book[nx][ny] = 1;

        }
    }
    cout << endl;
    return true;
}

int main() {
    //ifstream cin("G.in");
    while (cin >> n) {
        m = n;
        memset(G, 0, sizeof(G));
        memset(book, 0, sizeof(book));

        for (int x = 0; x < n; x++)
            for (int y = 0; y < m; y++) {
                cin >> G[x][y];
            }
        bfs(0, 0);

    }
    return 0;
}
